package com.yun.zf.app.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.HashMap;
import java.util.Map;

/**
 * @ Author     ：Wilson
 * @ Date       ：Created in 17:01 2018/12/19
 * @ Description：${description}
 * @ Modified By：
 * @Version: $version$
 */
public class MpGenerator {
    private static String url = "jdbc:mysql://localhost:3306/ds7?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
    private static String user = "root";
    private static String password = "5tgb^YHN";
    private static String dirverName = "com.mysql.jdbc.Driver";
    private static String author = "cj";
    private static String outputDir = "src/main/java"; //文件生成路径
    private static String packageName = "com.yun.zf";//文件生成所放的包名

    public static void main(String[] args) {
        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(url)
                .setUsername(user)
                .setPassword(password)
                .setDriverName(dirverName);
        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true) // 全局大写命名 ORACLE 注意
                //.setEntityLombokModel(false)
                .setNaming(NamingStrategy.underline_to_camel) // 表名生成策略
                //"sc_ad_count","sc_advertise","sc_attribute_cat","sc_attribute_value","sc_balance_log","sc_banner","sc_certification","sc_classify","sc_collection","sc_images","sc_member_cat","sc_member_info","sc_member_point_log","sc_notice","sc_order_cart","sc_relation","sc_shop_info","sc_spu_info","sc_spu_info_detail","sc_third_auth","sc_user_auth_rel","sc_user_info","sc_user_member"
                //zf_city","zf_banner","zf_community","zf_community_detail","zf_community_picture","zf_house","zf_house_detail","zf_house_picture","zf_house_tag","zf_subway","zf_subway_station","zf_sudoku
                .setInclude(new String[] {"zf_report",}); // 指定要生成的表

        //全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false)  // 不需要ActiveRecord特性的请改为false
                .setEnableCache(false) // XML 二级缓存
                .setAuthor(author)
                .setOutputDir(outputDir) //生成路径
                .setFileOverride(true)  //是否覆盖
                .setServiceName("%sService")//自定义文件命名， %s 会自动填充表实体属性
                .setEntityName("%s");

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageName);

        // 注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };

        //调整xml 生成目录
/*       List<FileOutConfig> focList= new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return "src/main/resources/mybatis/" + tableInfo.getEntityName() + ".xml";
            }
        });
        cfg.setFileOutConfigList(focList);*/

        // 关闭默认 xml 生成，调整生成 至 根目录
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);  // 上面调整xml 生成目录，这里一定要关闭才能生效

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo( pc
                ).setCfg(cfg)
                .setTemplate(tc)
                .execute();
    }

}
